﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using GameSimulator.Model;
using System.Threading;
using GameSimulator.ServiceReference1;
using GameSimulator.StudentServiceRef;
using System.IO.IsolatedStorage;

namespace GameSimulator.pages
{
    public partial class playGame : PhoneApplicationPage
    {
        public static int student_id=5;
        bool doGenerate = true;
        bool gameCompletionStatus = false;
        public int count = 0;
        public Game game;
        public List<Achievement> achievements;
        public StudentProgress studentProgress;
        private EventWaitHandle wh = new AutoResetEvent(true);
        public Thread genDataThread;
        public byte[] deviceID;
        int gameID, studentID, studentProgressID; 


        public playGame(int gameID, int studentID, int studentProgressID)
        {
            this.gameID = gameID;
            this.studentID = studentID;
            this.studentProgressID = studentProgressID;
            deviceID = utils.Device.GetDeviceUniqueID();
            initiateObjects();
            InitializeComponent();
        }

        public playGame()
        {
           
        }


        public void initiateObjects()
        {
            achievements = new List<Achievement>();
            studentProgress= new StudentProgress();
            game = new Game(1, "Cute Ball",GameSubjectType.Mathematic, GameType.Individual, 10);
            studentProgress.gameCompletionStatus = GameCompletionStatus.incompleted;
        }

        public void genData()
        {

            

            while (doGenerate && !studentProgress.gameCompletionStatus.ToString().Equals("completed"))
            {
                Random random = new Random();
                int randomNumber = random.Next(0, 20);
                int achievementType = random.Next(0, 6);
                generateAchievement(4);
            
                System.Threading.Thread.Sleep((int)System.TimeSpan.FromSeconds(1).TotalMilliseconds);
                Console.WriteLine("eRER");
                studentProgress.numberOfAnsweredQuestion++;

                 if(studentProgress.numberOfAnsweredQuestion==game.numberOfQuestion)
                     studentProgress.gameCompletionStatus=GameCompletionStatus.completed;
            }
  
        }

        public void generateAchievement(int achievementType)
        {
            
            Achievement ach;
            switch (achievementType)
            {
                
                case 1:
                    {
                        Random rn = new Random();
                        ach = new AchievementIntType(rn.Next(-30000, 30000), utils.DataGenerator.GetRandomNumber(0, 10), DateTime.Now);
                        achievements.Add(ach);
                        break;
                    }
                case 2:
                    {
                        ach = new AchievementDoubleType(utils.DataGenerator.GetRandomNumber(-30000, 30000), utils.DataGenerator.GetRandomNumber(0, 10), DateTime.Now);
                        achievements.Add(ach);
                        break;
                    }
                case 3:
                    {
                        ach = new AchievementStringType(utils.DataGenerator.getRandomString(), utils.DataGenerator.GetRandomNumber(0, 10), DateTime.Now);
                        achievements.Add(ach);
                        break;
                    }
                case 4:
                    {
                        //ach = new AchievementPicType(utils.DataGenerator.getRandomImage(), utils.DataGenerator.GetRandomNumber(-30000, 30000), DateTime.Now);
                        ach = new AchievementStringType(utils.DataGenerator.getRandomString(), utils.DataGenerator.GetRandomNumber(0, 10), DateTime.Now);
                        achievements.Add(ach);
                        break;
                    }
                case 5:
                    {
                        ach = new AchievementStringType();
                        achievements.Add(ach);
                        break;
                    }
            
             }
            
        }

        private void playGameButton_Click(object sender, RoutedEventArgs e)
        {

            //Style style = (Style)App.Current.Resources["PerformanceProgressBar"];
            //if (style == null) { throw new InvalidOperationException("The style was not found."); }
            //ProgressBar bar = new ProgressBar
            //{
            //    IsIndeterminate = true,
            //    Style = style,
            //    Margin = new Thickness(100, 0, 0, 256)
            //};
            //LayoutRoot.Children.Add(bar);

            

            genDataThread = new Thread(new ThreadStart(genData));
            doGenerate = true;
            count = 0;
            genDataThread.Start();
            //MessageBox.Show("starting");
            while (!genDataThread.IsAlive) ;
            Thread.Sleep(1);
            //MessageBox.Show("after Thread");

        }
        public void RequestStop()
        {
            doGenerate = false;
        }

        private void pauseGameButton_Click(object sender, RoutedEventArgs e)
        {
            RequestStop();
            genDataThread.Join();
            studentProgress.achievements=achievements;
            achievement_listBox.ItemsSource = achievements;
            //MessageBox.Show(count.ToString());

        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            //User userObj = new User();
            //GameDataAccessClient gameDataAccessClient = new GameDataAccessClient();
            //gameDataAccessClient.saveUserCompleted += new EventHandler<saveUserCompletedEventArgs>(GameDataAccessClient_saveUserCompleted);

            //var GameDataAccessClient = new GameDataAccessClient();
            //GameDataAccessClient.saveUserCompleted += new EventHandler<saveUserCompletedEventArgs>(GameDataAccessClient_saveUserCompleted);
            //GameDataAccessClient.saveUserAsync(user);
            //GameDataAccessClient proxy = new GameDataAccessClient();
            //proxy.saveUserAsync(userObj);

            StudentSrvClient studentSrvClient = new StudentSrvClient();
            studentSrvClient.addCurrentStudentCompleted += new EventHandler<addCurrentStudentCompletedEventArgs>(StudentServiceClient_saveUserCompleted);
            studentSrvClient.addCurrentStudentAsync(5,deviceID);


            Achievement aa = new AchievementStringType();
            aa.xp = 11;

            if (IsolatedStorageSettings.ApplicationSettings.Contains("amin"))
                IsolatedStorageSettings.ApplicationSettings.Remove("amin");
            aa.achivementType = AchivementType.StringAchievement;
            IsolatedStorageSettings.ApplicationSettings.Add("amin", aa);
            IsolatedStorageSettings.ApplicationSettings.Save();

            Achievement bb = new AchievementStringType();
            bb = (AchievementStringType)IsolatedStorageSettings.ApplicationSettings["amin"];

            MessageBox.Show(bb.xp.ToString());

        }
        private void StudentServiceClient_saveUserCompleted(Object sender, addCurrentStudentCompletedEventArgs e)
        {
            MessageBox.Show(e.Result);
        }

        
    }
}